package com.cyz.admin.config;

import com.cyz.admin.filter.JwtAuthenticationTokenFilter;
import com.cyz.common.security.AccessDeniedHandlerImpl;
import com.cyz.common.security.AuthenticationEntryPointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * SpringSecurity配置文件
 * 作用：  用来规定那些路径可以不进行检测
 * 重写数据库填入密码类数据的加密与解密方式
 * 配置security自定义细节
 * 向外暴露AuthenticationManager
 */

@Configuration
//开启权限验证注解，@PreAuthorize 和 @PostAuthorize 两个注解
//@PreAuthorize 注解会在方法执行前进行验证，而 @PostAuthorize 注解会在方法执行后进行验证。
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Autowired
    private AccessDeniedHandlerImpl accessDeniedHandler;
    @Autowired
    private AuthenticationEntryPointImpl authenticationEntryPoint;

    /**
     * 白名单
     */
//    private static final String[] WHITE_FORM = {
//            "/user/login",
//            "/swagger-resources/**",
//            "/v3/**",
//            "/swagger-ui/**"
//    };

    /**
     * 重写数据库填入密码类数据的加密与解密方式
     * 初始默认很麻烦
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 配置security
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭跨域防火墙
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
//                .and()
//                .authorizeRequests()
                // 对于登录接口 允许匿名访问
//                .antMatchers(WHITE_FORM).permitAll()
                // 除上面外的所有请求全部不需要认证即可访问
//                .anyRequest().permitAll()
                //.anyRequest().authenticated()
                .and()
                //关闭默认退出接口
                .logout().disable()
                //允许跨域
                .cors()
                //把jwtAuthenticationTokenFilter添加到SpringSecurity的过滤器链中
                .and()
                //添加过滤器
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
                //添加错误处理器
                .exceptionHandling()
                //认证错误处理器
                .authenticationEntryPoint(authenticationEntryPoint)
                //授权错误处理器
                .accessDeniedHandler(accessDeniedHandler);

    }

    /**
     * 向外暴露AuthenticationManager
     */
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
